<script setup>
import { onMounted, ref } from 'vue'
import { artGetChannelsService } from '@/api/articel'
import { Edit, Delete } from '@element-plus/icons-vue'

// 导入删除文章管理的Api
import { artDelChannelService } from '@/api/articel.js'
import { ElMessageBox } from 'element-plus'

// 导入组件
import ArticleDiolog from '@/views/article/components/ArticleDiolog.vue'
const channelList = ref([])
// 发送请求获取用户的文章数据
const getChannelList = async () => {
  const res = await artGetChannelsService()
  channelList.value = res.data.data
}
onMounted(() => {
  getChannelList()
})

// 弹窗组件
const dialog = ref() //获取组件的方法
const onAddChannel = () => {
  dialog.value.open({})
}
const onEditChannel = (row) => {
  dialog.value.open(row)
}
// 子传父 --- 刷新页面 重新调用 getChannelList（）
const onSuccess = () => {
  getChannelList()
}

// 删除按钮
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  getChannelList()
  console.log(row.id)
}
</script>

<template>
  <PageContainer title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel()"> 添加分类</el-button>
    </template>
    <el-table :data="channelList" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name">
        <template #default="{ row }">
          <el-link type="warning">{{ row.cate_name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row)"
          >
          </el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row)"
          >
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <ArticleDiolog ref="dialog" @success="onSuccess"></ArticleDiolog>
  </PageContainer>
</template>
